package com.l024.android_ljl.repository;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.api.AppApi;
import com.l024.android_ljl.cache.CacheManager;
import com.l024.android_ljl.entity.ObjectEntity;
import com.l024.android_ljl.entity.SysCommentEntity;
import com.l024.android_ljl.entity.SysSubjectEntity;
import com.l024.android_ljl.entity.SysTypeEntity;
import com.l024.android_ljl.entity.SysUserEntity;
import com.l024.android_ljl.entity.SysUserSaveEntity;
import com.quantstack.llcommonlibrary.base.BaseHttpResult;
import com.quantstack.llcommonlibrary.base.BaseRepository;
import com.quantstack.llhttplibrary.EasyHttp;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/5/6 17:45
 * @Notes
 */
public class AppRepository extends BaseRepository {
    private AppApi appApi;

    public AppRepository() {
        appApi = EasyHttp.getInstance().create(AppApi.class);
    }


    /**
     * 登录
     */
    public Observable<SysUserEntity> login(String phone, String password){
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setPhone(phone);
        sysUserEntity.setPassword(password);
        String json = JSON.toJSONString(sysUserEntity);
        return appApi.login(createJsonRequestBody(json));
    }

    /**
     * 注册
     */
    public Observable<BaseHttpResult<ObjectEntity>> register(SysUserEntity sysUserEntity){
        if(sysUserEntity!=null){
            String json = JSON.toJSONString(sysUserEntity);
            return appApi.register(createJsonRequestBody(json));
        }
        return null;
    }

    /**
     * 上传图片
     */
    public Observable<BaseHttpResult<ObjectEntity>> uploadFile(String url){
        File file = new File(url);
        if(file.exists()){
            RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("file",file.getName(),RequestBody.create(MediaType.parse("image/*"),file)).build();
           return appApi.uploadFile(requestBody);
        }else{
            ToastUtils.show("文件不存在,请重新上传");
        }
        return null;
    }

    /**
     * 获取所有的试题
     */
    public Observable<BaseHttpResult<List<SysSubjectEntity>>> getSubjects(){
        return appApi.getSubjects();
    }

    /**
     * 获取类型下所有的试题
     */
    public Observable<BaseHttpResult<List<SysSubjectEntity>>> getTypeSubjects(Long id){
        if(id>0){
            return appApi.getTypeSubjects(id);
        }
        return null;
    }

    /**
     * 获取所有的类型
     */
    public Observable<BaseHttpResult<List<SysTypeEntity>>> getTypes(){
        return appApi.getTypes();
    }

    /**
     * 添加用户收藏
     */
    public Observable<ObjectEntity> addUserSave(long subjectId){
        SysUserEntity cacheUser = CacheManager.getInstance().getCacheUser();
        if(cacheUser!=null){
            Map<String,Long> map = new HashMap<>();
            map.put("userId",cacheUser.getId());
            map.put("subjectId",subjectId);
            String json = JSON.toJSONString(map);
            return appApi.saveUserSubject(createJsonRequestBody(json));
        }
        return null;
    }

    /**
     * 获取用户所有收藏
     */
    public Observable<BaseHttpResult<List<SysUserSaveEntity>>> getUserSaves(){
        SysUserEntity cacheUser = CacheManager.getInstance().getCacheUser();
        if(cacheUser!=null){
            return appApi.getUserSave(cacheUser.getId());
        }
        return null;
    }

    /**
     * 添加用户收藏
     */
    public Observable<ObjectEntity> addComment(long subjectId,String comment){
        SysUserEntity cacheUser = CacheManager.getInstance().getCacheUser();
        if(cacheUser!=null&&!TextUtils.isEmpty(comment)){
            Map<String,Object> map = new HashMap<>();
            map.put("userId",cacheUser.getId());
            map.put("subjectId",subjectId);
            map.put("comment",comment);
            String json = JSON.toJSONString(map);
            return appApi.saveComment(createJsonRequestBody(json));
        }
        return null;
    }

    /**
     * 获取用户所有评论
     */
    public Observable<BaseHttpResult<List<SysCommentEntity>>> getUserComments(){
        SysUserEntity cacheUser = CacheManager.getInstance().getCacheUser();
        if(cacheUser!=null){
            return appApi.getUserComment(cacheUser.getId());
        }
        return null;
    }

    /**
     * 获取试卷所有评论
     */
    public Observable<BaseHttpResult<List<SysCommentEntity>>> getSubjectComments(long subjectId){
        if(subjectId>0){
            return appApi.getSubjectComment(subjectId);
        }
        return null;
    }


    /**
     * 搜索试题
     */
    public Observable<BaseHttpResult<List<SysSubjectEntity>>> searchSubject(String key){
        if(!TextUtils.isEmpty(key)){
            return appApi.searchSubject(key);
        }
        return null;
    }

}
